package indi.jp.project.business.jingying.controller;

import indi.jp.project.core.entity.JsonResult;
import indi.jp.project.core.util.SimpleEntityUtil;
import indi.jp.project.business.jingying.entity.JyProject;
import indi.jp.project.business.jingying.entity.JyProjectTrace;
import indi.jp.project.business.jingying.service.JyProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.Map;

@RestController
@RequestMapping("/jingying/project")
public class JyProjectController {

    @Autowired
    private JyProjectService jyProjectService;

    @GetMapping("/queryGrid")
    @ResponseBody
    public JsonResult queryGrid(@ModelAttribute JyProject entity, HttpServletRequest request){
        SimpleEntityUtil<JyProject> simpleEntityUtil = new SimpleEntityUtil<>();
        entity = simpleEntityUtil.setBaseEntity(entity, request);
        try {
            Map<String, Object> map = jyProjectService.queryGrid(entity);
            return JsonResult.success(map);
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @PostMapping("/save")
    @ResponseBody
    public JsonResult save(@RequestBody JyProject entity, HttpServletRequest request) {
        SimpleEntityUtil<JyProject> simpleEntityUtil = new SimpleEntityUtil<>();
        entity = simpleEntityUtil.setBaseEntity(entity, request);
        try {
            return jyProjectService.save(entity);
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @PostMapping("/deleteById")
    @ResponseBody
    public JsonResult deleteById(HttpServletRequest request) {
        String id = request.getParameter("id");
        try {
            jyProjectService.deleteById(id);
            return JsonResult.success("");
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @GetMapping("/queryProjectTraceGrid")
    @ResponseBody
    public  JsonResult queryProjectTraceGrid(@ModelAttribute JyProjectTrace entity, HttpServletRequest request){
        SimpleEntityUtil<JyProjectTrace> simpleEntityUtil = new SimpleEntityUtil<>();
        entity = simpleEntityUtil.setBaseEntity(entity, request);
        try {
            Map<String, Object> map = jyProjectService.queryProjectTraceGrid(entity);
            return JsonResult.success(map);
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @PostMapping("/saveProjectTrace")
    @ResponseBody
    public JsonResult saveProjectTrace(@RequestBody JyProjectTrace entity, HttpServletRequest request) {
        SimpleEntityUtil<JyProjectTrace> simpleEntityUtil = new SimpleEntityUtil<>();
        entity = simpleEntityUtil.setBaseEntity(entity, request);
        try {
            int effectRow = jyProjectService.saveProjectTrace(entity);
            if(effectRow > 0){
                return JsonResult.success("");
            } else {
                return JsonResult.failure("保存失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @GetMapping("/queryDetailById")
    @ResponseBody
    public JsonResult queryDetailById(HttpServletRequest request){
        String id = request.getParameter("id");
        try {
            JyProject entity = jyProjectService.queryDetailById(id);
            return JsonResult.success(entity);
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @PostMapping("/deleteProjectTraceById")
    @ResponseBody
    public JsonResult deleteProjectTraceById(HttpServletRequest request) {
        String id = request.getParameter("id");
        try {
            int effectRow = jyProjectService.deleteProjectTraceById(id);
            if(effectRow > 0){
                return JsonResult.success("");
            } else {
                return JsonResult.failure("未能成功删除，请刷新查看数据是否存在");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }

    }

}
